﻿using iTextSharp.text.pdf.parser.clipper;
using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// VideoView.xaml 的交互逻辑
    /// </summary>
    public partial class VideoView : Window
    {
        private uvcham cam_ = null;
        private WriteableBitmap bmp_ = null;
        private int width_ = 0, height_ = 0;
        private int count_ = 0;
        private Timer timer_ = null;
        public VideoView()
        {
            InitializeComponent();
        
    }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.Filter = "ASF File|*.asf|MP4 File|*.mp4|MKV File|*.mkv";
            bool? result = dialog.ShowDialog(); // 在WPF中，ShowDialog返回的是bool?类型

            if (result == true) // WPF中判断对话框是否点击了确定
            {
                cam_.record(dialog.FileName); // 开始录制
                //recordBtn.Content = "Stop"; // 设置按钮的文本，WPF中是使用Content而不是Text
            }
        }

        private void start_Click(object sender, RoutedEventArgs e)
        {
            if (cam_ != null)
                return;
            uvcham.Device[] arr = uvcham.Enum();
            if (arr.Length <= 0)
                HandyControl.Controls.MessageBox.Show("not found");
            else
            {
                cam_ = uvcham.open(arr[0].id);
                if (cam_ != null)
                {
                    cam_.put(uvcham.eCMD.WBMODE,2);
                    cam_.get(uvcham.eCMD.WIDTH|0, out width_);
                    cam_.get(uvcham.eCMD.HEIGHT|0,out height_);
                    if ((width_ > 0) && (height_ > 0))
                    {
                        cam_.put(uvcham.eCMD.FLIPVERT, 1);
                        bmp_ = new WriteableBitmap(width_, height_, 0, 0, PixelFormats.Bgr32, null);

                        if (cam_.start(IntPtr.Zero, (nEvent) => 
                        {
                            Dispatcher.BeginInvoke((Action)(() => 
                            {
                                if (cam_ != null)
                                {
                                    if ((nEvent & uvcham.eEVENT.IMAGE) != 0)
                                        OnEventImage();
                                }
                            }));

                        }) < 0)
                        { 
                        }
                    }
                    
                }
            }
        }

        private void OnEventImage()
        {
            if (bmp_ != null)
            {
                try
                {
                    bmp_.Lock();
                    try
                    {
                        cam_.pull(bmp_.BackBuffer);/* Pull Mode */
                        bmp_.AddDirtyRect(new Int32Rect(0, 0, bmp_.PixelWidth, bmp_.PixelHeight));
                    }
                    finally
                    {
                        bmp_.Unlock();
                    }
                    image_.Source = bmp_;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
